<template>
    <div>
        <!-- 分割线 -->
        <el-divider></el-divider>
        <!-- 新增表单 -->
        <el-form ref="form" :rules="rules" :model="form" label-width="100px" style="width: 300px;">
            <el-form-item label="演员姓名" prop="actorName">
                <el-input v-model="form.actorName" placeholder="请输入姓名"></el-input>
            </el-form-item>
            <el-form-item label="选择头像" prop="actorAvatar">
                <el-upload class="avatar-uploader" :show-file-list="false" :action="`${$UPLOADURL}/upload`"
                    :on-success="handleAvatarSuccess">
                    <img v-if="form.actorAvatar" :src="form.actorAvatar" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">新增演员</el-button>
                <el-button>返回</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import myaxios from '@/http/MyAxios';
export default {
    data() {
        return {
            form: {
                actorName: "", // 绑定演员姓名
                actorAvatar: '', // 绑定头像
            },
            rules: {
                actorName: [
                    { required: true, message: '姓名必须填写', trigger: 'blur' }
                ],
                actorAvatar: [
                    { required: true, message: '图片必须上传' }
                ]
            },
        };
    },
    methods: {
        // 提交添加演员列表
        onSubmit() {
            // 验证表单
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    //  valid返回true意味着两个字段验证通过
                    // post请求，执行添加业务
                    let params = this.form
                    this.$httpApi.actorsApi.add(params).then(res => {
                       if(res.data.code==200){
                        // 重置表单
                        this.$refs['form'].resetFields()
                        // 成功提示
                        this.$message({
                            message:'新增演员成功',
                            type:'success'
                        })
                       }
                    })
                } else {
                    return false
                }
            })

        },
        // 上头像成功后自动调用，将会自动传入响应结果res与文件对象file
        handleAvatarSuccess(res, file) {
            console.log(res)
            this.form.actorAvatar = res.data
        },
    },
};
</script>

<style lang="scss" >
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 158px;
    height: 158px;
    line-height: 158px;
    text-align: center;
}

.avatar {
    width: 158px;
    height: 158px;
    display: block;
}
</style>